<?php


namespace Buu\Modules\EventSystem\Controllers;

use App\Models\DbInstance;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class DbInstanceController extends AdminController
{
    protected $title = '数据库实例';

    public function index(Content $content)
    {
        $content->breadcrumb([
            'text' => $this->title
        ]);
        return parent::index($content);
    }

    protected function grid()
    {
        $grid = new Grid(new DbInstance);
        $grid->paginate(50);
        $grid->disableFilter();
        $grid->disableExport();
        $grid->column('id', 'ID');
        $grid->column('name', '实例名称');
        $grid->column('replication_host', '数据库地址');
        $grid->column('replication_port', '端口');
        $grid->column('replication_user', '用户名');
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            if ($this->row['delete_time'] != 0) {
                $actions->append("<span style=\"color:red\">已删除</span>");
                $actions->disableDelete();
                $actions->disableEdit();
            }
        });
        return $grid;
    }

    protected function form()
    {
        $form = new Form(new DbInstance);
        $form->footer(function (Form\Footer $footer) {
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
            $footer->disableViewCheck();
        });
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->text('name', '名称')
            ->placeholder('请输入名称')->setWidth(4, 2)->required('');
        $form->text('replication_host', '数据库地址')->setWidth(4, 2)->required();
        $form->number('replication_port', '数据库端口')->setWidth(4, 2)->default('3306')->required();
        $form->text('replication_user', '数据库用户名')->setWidth(4, 2)->required();
        $form->password('replication_password', '数据库密码')->setWidth(4, 2)->required();
        $form->radio('schema_type', '指定schema实例')
            ->help('默认情况下获取数据库schema信息的实例与获取Binlog的实例相同，特殊情况下可自行指定')
            ->options([
                1 => '否',
                2 => '是'
            ])
            ->default(1)
            ->when(2, function (Form $form) {
                $form->text('schema_host', 'schema数据库地址')
                    ->setWidth(4, 2);
                $form->number('schema_port', 'schema数据库端口')
                    ->setWidth(4, 2)->default('3306');
                $form->text('schema_user', 'schema数据库用户名')
                    ->setWidth(4, 2);
                $form->password('schema_password', 'schema数据库密码')
                    ->setWidth(4, 2);
            });
        $form->saving(function (Form $form) {
            if ($form->schema_type == 1) {
                $form->schema_host = $form->replication_host;
                $form->schema_port = $form->replication_port;
                $form->schema_user = $form->replication_user;
                $form->schema_password = $form->replication_password;
            }
        });
        return $form;
    }

    public function getDatabases(Request $request)
    {
        $instanceId = $request->get('q');

        return DbInstance::getDatabasesById($instanceId, 1);
    }

    public function getTables(Request $request)
    {
        $q = $request->get('q');
        $params = explode(':', $q);

        return DbInstance::getTablesById($params[0],$params[1], 1);
    }
}